package com.lm.delayqueue.example;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class OrderTask implements Delayed {
    private final String orderId;
    private final long expireTime; // 到期时间戳

    public OrderTask(String orderId, long delay, TimeUnit unit) {
        this.orderId = orderId;
        this.expireTime = System.currentTimeMillis() + unit.toMillis(delay);
    }

    public String getOrderId() {
        return orderId;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long remaining = expireTime - System.currentTimeMillis();
        return unit.convert(remaining, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if (o instanceof OrderTask) {
            return Long.compare(this.expireTime, ((OrderTask) o).expireTime);
        }
        return 0;
    }
}
